/**
 * 联系人管理初始化
 */
var Contact = {
    id: "ContactTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Contact.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '权限', field: 'authority', visible: false, align: 'center', valign: 'middle'},
        {title: '姓名', field: 'name', visible: true, align: 'center', valign: 'middle'},
        {title: '客户名称', field: 'customerCompanyName', visible: true, align: 'center', valign: 'middle'},
        {title: '部门名称', field: 'departName', visible: true, align: 'center', valign: 'middle'},
        {title: '岗位职务', field: 'post', visible: true, align: 'center', valign: 'middle'},
        {title: '手机号', field: 'mobileNum', visible: true, align: 'center', valign: 'middle'},
        {title: '备用手机号', field: 'standbyMobileNum', visible: true, align: 'center', valign: 'middle'},
        {title: '办公电话', field: 'landline', visible: true, align: 'center', valign: 'middle'},
        {title: '传真', field: 'facsimile', visible: true, align: 'center', valign: 'middle'},
        {title: '电子邮箱', field: 'email', visible: true, align: 'center', valign: 'middle'},
        {title: '微信号', field: 'wechat', visible: true, align: 'center', valign: 'middle'},
        {title: 'qq号', field: 'qq', visible: true, align: 'center', valign: 'middle'},
        {title: '态度', field: 'attitude', visible: true, align: 'center', valign: 'middle'},
        {title: '备注', field: 'remark', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
Contact.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Contact.seItem = selected[0];
        return true;
    }
};

Contact.checkPermission = function(){
    if (this.check()) {
        console.log(Contact.seItem.authority);
        if (Contact.seItem.authority === 1) {
            return true
        }else{
            Feng.info("您没有该操作权限！");
        }
    }
};

/**
 * 点击添加联系人
 */
Contact.openAddContact = function () {
    var index = layer.open({
        type: 2,
        title: '添加联系人',
        area: ['800px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/contact/contact_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看联系人详情
 */
Contact.openContactDetail = function () {
    if (this.checkPermission()) {
        var index = layer.open({
            type: 2,
            title: '联系人详情',
            area: ['800px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/contact/contact_update/' + Contact.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除联系人
 */
Contact.delete = function () {
    if (this.check()) {
        var operation = function () {
        var ajax = new $ax(Feng.ctxPath + "/contact/delete", function (data) {
            Feng.success("删除成功!");
            Contact.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("contactId", Contact.seItem.id);
        ajax.start();
    }
    Feng.confirm("是否删除联系人" + Contact.seItem.name + "?", operation);
}
};
Contact.resetSearch = function(){
    $("#name").val("");
    $("#customerCompanyName").val("");
    Contact.search();
}

/**
 * 查询联系人列表
 */
Contact.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['customerCompanyName'] = $("#customerCompanyName").val();
    Contact.table.refresh({query: queryData});
};


Contact.exportXlsByT = function () {
    window.location.href = Feng.ctxPath + "/contact/exportXlsByM";
}

$(function () {
    var defaultColunms = Contact.initColumn();
    var table = new BSTable(Contact.id, "/contact/list", defaultColunms);
    table.setPaginationType("server");
    Contact.table = table.init();

    var upload = new $WebUpload({
        uploadId: "upload",
        mimeTypes: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel",
        extensions: "xlsx,xls",
        url: "/contact/importExcel",
        multiple: false,
    });
    upload.init();
});
